﻿using System; 
using System.Data;
using System.Collections.Generic; 
using JOIN.Rights.Model;
using Kevin.Framework.Data;
using JOIN.Rights.BLL.Cache;

/*------------------------------------------------------------------------
   苏州捷安信息科技有限公司 版权所有			
   文件名: TU_Menus.cs
   作 者 : 席吉波
   日 期 : 2012-10-19
   备 注 : 当您对本文件更改时请写明修改人、修改日期以及事由
  ------------------------------------------------------------------------
*/

namespace JOIN.Rights.BLL
{
	 	//TU_Menus
		/// <summary>
	/// Menus 业务操作类
	/// </summary>
	public partial class MenusBLL:BLLBase<MenusInfo>,ISelect<MenusInfo>
	{
		#region   构造函数
		
		/// <summary>
		/// MenusBLL  构造函数  
		/// </summary>
		public MenusBLL(MenusInfo entity)
			:base(entity)
		{
		
		}
		/// <summary>
		/// MenusBLL  构造函数  
		/// </summary>
		/// <param name="operationMode">操作模式</param>
        /// <param name="jsonEntity">前台javascript传过来的JSON实体</param>
		public MenusBLL(string operationMode, string jsonEntity)
        {
            OperState cmd = (OperState)Enum.Parse(typeof(OperState), operationMode);
            MenusInfo entity = ConvertJSON2Entity(jsonEntity);
            base.OperState = cmd;
            base.Entity = entity;
            base.ServiceResult = new ServiceResult<MenusInfo>();
            base.ServiceResult.Successed = true;
            base.ServiceResult.Object = base.Entity;
        }
        
        #endregion
        
        #region 查询方法
        
        /// <summary>
        /// 获取 MenusInfo 集合
        /// </summary>
        /// <param name="pageSize">分页记录数</param>
        /// <param name="pageIndex">页码索引</param>
        /// <returns></returns>
        public ServiceResult<MenusInfo> SelectByProperties(int pageSize, int pageIndex)
        {
            this.ServiceResult = new ServiceResult<MenusInfo>();
            try
            {
                int recordCount =0;
                List<MenusInfo> list = base.Entity.SelectedByProperties<MenusInfo>(out recordCount,pageSize,pageIndex);
                
                ServiceResult.Successed = true;
                ServiceResult.RecordCount = recordCount;
                ServiceResult.List = list;
                
            }
            catch(Exception ex){
                ServiceResult.ErrorMsg = "数据查询失败:"+ex.Message;
                ServiceResult.Successed = false;
                ServiceResult.RecordCount = 0;
                ServiceResult.List = null;
            }

            return ServiceResult;
        }
        List<MenusInfo> Menus = new List<MenusInfo>();

        public List<MenusInfo> Load(string projectID, string parentID, List<string> funcList)
        {   
            List<MenusInfo> result = new List<MenusInfo>();
            MenusInfo current = new MenusInfo();
            current.ProjectID=projectID;
            current.MenuParent = parentID;

            MenusInfo temp=(MenusInfo)current.Clone();
            Load(temp, projectID, parentID);
            //result = ((MenusInfo)temp.Clone()).SubMenu;

            //if (funcList.Count > 0)
            //{
                //result = ((MenusInfo)temp.Clone()).SubMenu;
                LoadInner(result, temp.SubMenu, funcList);
                //result.RemoveAll(o => funcList.Contains(o.FunctionID));
            //}

            return result;
        }

        private void LoadInner(List<MenusInfo> result, List<MenusInfo> menus, List<string> funcList)
        {
            if (funcList.Count == 0) return;

            foreach (MenusInfo menu in menus)
            {
                if (funcList.Contains(menu.FunctionID))
                {
                    MenusInfo item = (MenusInfo)menu.Clone();
                    result.Add(item);
                    
                    if (menu.SubMenu != null)
                    {
                        item.SubMenu = new List<MenusInfo>();
                        LoadInner(item.SubMenu, menu.SubMenu, funcList);
                    }
                }
            }
            //return result;
        }

        public List<MenusInfo> Load(MenusInfo current, string projectID, string parentID)
        {
            List<MenusInfo> result = null;

            if (!MenusCache.Instance.TryGetValue(projectID, out result))
            {
                result = LoadInner(current, projectID, parentID);

                MenusCache.Instance.Add(projectID, result);
            }
            else
            {
                current.SubMenu = result;
            }
            return result;
        }

        private List<MenusInfo> LoadInner(MenusInfo current, string projectID, string parentID)
        {
            List<MenusInfo> subMenu = new List<MenusInfo>();

            MenusInfo menuItem = new MenusInfo();
            menuItem.ProjectID = projectID;
            menuItem.MenuParent = parentID;

            subMenu = menuItem.SelectedByProperties<MenusInfo>();

            current.SubMenu = subMenu;
            foreach (MenusInfo item in subMenu)
            {
                LoadInner(item, item.ProjectID, item.MenuID);
            }

            return subMenu;
        }

        private void FindSub(ref MenusInfo item)
        {
            List<MenusInfo> subItems = new List<MenusInfo>();
            subItems = new MenusInfo() { ProjectID = item.ProjectID, MenuParent = item.MenuID }.SelectedByProperties<MenusInfo>();
            foreach (MenusInfo subItem in subItems)
            {
                subItems.Add(subItem);
            }
        }
        
        #endregion
        
        #region 重写基类方法
        
        
        
        #endregion
   
	}
}